package com.piece.member.service;

import com.piece.member.model.UmsMemberLevel;
import com.piece.member.repository.UmsMemberLevelRepository;
import com.piece.core.jpa.repository.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;

@Service
@Transactional
public class UmsMemberLevelService extends BaseService<UmsMemberLevel, String> {

    @Autowired
    private UmsMemberLevelRepository umsMemberLevelRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostConstruct
    public void initRepository() {
        setRepository(umsMemberLevelRepository);
    }

    /**
     * 更新会员等级特权
     */
    public void change(String id, String type, UmsMemberLevel level) {
        String column = "";
        Object value = null;
        switch (type) {
            case "status":
                umsMemberLevelRepository.executeSql("update ums_member_level set default_status =?0 where id !=?1",
                        new Object[]{ "1", id });
                column = "default_status";
                value = level.getDefaultStatus();
                break;
            case "freight":
                column = "privilege_free_freight";
                value = level.getPrivilegeFreeFreight();
                break;
            case "price":
                column = "privilege_member_price";
                value = level.getPrivilegeMemberPrice();
                break;
            case "birthday":
                column = "privilege_birthday";
                value = level.getPrivilegeBirthday();
                break;
        }

        String sql = "update ums_member_level set " + column + " = ?0 where id = ?1";
        umsMemberLevelRepository.executeSql(sql, new Object[]{ value, id });
    }
}
